package com.dataimport.service.impl;

import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dataimport.entity.sourceentity.DepartmentInfo;
import com.dataimport.entity.sourceentity.StudentBaseInfo;
import com.dataimport.entity.targetentity.Department;
import com.dataimport.entity.targetentity.SchoolCalendar;
import com.dataimport.mapper.DepartmentInfoMapper;
import com.dataimport.service.DepartmentInfoService;

import com.dataimport.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author xin
 * @date 2022/9/27 10:10
 */
@Service
public class DepartmentInfoServiceImpl extends ServiceImpl<DepartmentInfoMapper, DepartmentInfo> implements DepartmentInfoService {

    @Autowired
    private DepartmentInfoMapper departmentInfoMapper;

    @Autowired
    private DepartmentService departmentService;

    public List<Department> selectData(Long currentPage){
        //开启数据库2
        DynamicDataSourceContextHolder.push("mysql2");
        //查询全部的院系即单位
        Page<DepartmentInfo> page = new Page<>(currentPage,1000);
        Page<DepartmentInfo> departmentInfoPage = departmentInfoMapper.selectPage(page, null);
        List<DepartmentInfo> departmentInfos = departmentInfoPage.getRecords();
        //过滤数据，复制所需字段
        List<Department> departments = departmentInfos.stream().parallel()
                .filter(departmentInfo -> StringUtils.hasText(departmentInfo.getDWMC()))
                .map(departmentInfo -> {
                    Department department = new Department();
                    department.setDepartmentCode(departmentInfo.getDWBM());
                    department.setDepartmentName(departmentInfo.getDWMC());
                    department.setDepartmentParentCode(departmentInfo.getLSDWH());
                    department.setDepartmentStatus(Integer.valueOf(departmentInfo.getDWYXBS()));
                    return department;
                })
                .collect(Collectors.toList());

        departments.forEach(department -> System.out.println(department));
        DynamicDataSourceContextHolder.poll();

        return departments;
    }

    //处理院系即单位表
    @Override
    public void dealWithDepartmentInfo() {
        long currentPage = 0L;
        while (true) {
            currentPage++;
            List<Department> departments = selectData(currentPage);
            if (ObjectUtils.isEmpty(departments)) {
                break;
            }
            insert(departments);
            System.out.println("插入成功");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void insert(List<Department> departments){
        //插入数据
        DynamicDataSourceContextHolder.push("mysql1");
        departmentService.saveBatch(departments);
        DynamicDataSourceContextHolder.poll();
    }
}
